/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oktago.ibore;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author massimo
 */
public class SubTypesMenuAdapter extends BaseAdapter implements OnItemClickListener {

    DetailActivity detailActivity;
    IBoreApplication iBoreApplication;
    boolean showLoginIcon;
    JSONArray subTypes;
    ArrayList<CustomFunction> customFunctions;
    Long idRoot;

    public SubTypesMenuAdapter(DetailActivity detailActivity, JSONArray subTypes, ArrayList<CustomFunction> customFunctions, Long idRoot) {
        this.idRoot=idRoot;
        showLoginIcon=idRoot == null;
        this.detailActivity = detailActivity;
        this.iBoreApplication = (IBoreApplication) detailActivity.getApplication();
        this.subTypes = subTypes;
        this.customFunctions=customFunctions;
        if (customFunctions==null) {
            customFunctions=new ArrayList<CustomFunction>();
        }
    }

    public int getCount() {
        return subTypes.length() + customFunctions.size()+ (showLoginIcon?1:0);
    }

    public Object getItem(int position) {
        if (position < subTypes.length()) {
            try {
                return subTypes.getJSONObject(position);
            } catch (JSONException ex) {
                Logger.getLogger(SubTypesMenuAdapter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if (position<subTypes.length()+customFunctions.size()) {
            return customFunctions.get(position-subTypes.length());
        }
        return null;
    }

    public long getItemId(int position) {
        if (position < subTypes.length()) {
            try {
                JSONObject o = subTypes.getJSONObject(position);
                if (o.getInt("direct_link")==1) {
                    return -o.getLong("instance_id");
                }
                return o.getLong("composite_type");
            } catch (JSONException ex) {
                Logger.getLogger(SubTypesMenuAdapter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return Long.MAX_VALUE;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(detailActivity);
            imageView.setLayoutParams(new GridView.LayoutParams(56, 56));
            //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        Bitmap icon=null;
        if (position < subTypes.length()) {
            try {
                //restituisco l'icona del composite
                JSONObject o = subTypes.getJSONObject(position);
                String typeName = o.getString("composite_name");
                icon = iBoreApplication.getImageFromTypeName(typeName);
            } catch (JSONException ex) {
                Logger.getLogger(SubTypesMenuAdapter.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (position < subTypes.length() + customFunctions.size()) {
            icon=customFunctions.get(position-subTypes.length()).getIcon();
        } else if (showLoginIcon) {
            icon = iBoreApplication.getLoginLogoutImage();
        }
        imageView.setImageBitmap(icon);
        return imageView;
    }

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        if (position < subTypes.length()) {
            //TODO: controllare se devo caricare un oggetto o una lista
            //carico la ListAction del composite
            
            if (id>0) {
                Intent i = new Intent(detailActivity, ListActivity.class);
                i.putExtra("idt", id);
                if (idRoot!=null) {
                    i.putExtra("root", idRoot);
                }
                detailActivity.startActivity(i);
            }
            else {
                Intent i = new Intent(detailActivity, DetailActivity.class);
                i.putExtra("id", -id);
                detailActivity.startActivity(i);
            }
        } else if (position < subTypes.length() + customFunctions.size()) {
            //carico la Action della funzione specifica
            Intent i = customFunctions.get(position-subTypes.length()).getIntent(detailActivity);
            detailActivity.startActivity(i);
        } else {
            if (iBoreApplication.logged) {
                iBoreApplication.logout();
                detailActivity.startDownload();
            } else {
                //carico la LoginAction
                Intent i = new Intent(detailActivity, LoginActivity.class);
                detailActivity.startActivity(i);
            }
        }
    }
}
